//
//  AboutViewController.m
//  Contractor
//
//  Created by 金鑫 on 16/5/3.
//  Copyright © 2016年 star j. All rights reserved.
//

#import "AboutViewController.h"
#import "ServiceViewController.h"
#import "CONST.h"

@interface AboutViewController ()

@end

@implementation AboutViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.edgesForExtendedLayout = UIRectEdgeBottom | UIRectEdgeLeft | UIRectEdgeRight;
    
    //平台图片
    self.view.backgroundColor = [UIColor whiteColor] ;
    UIImage *pingtaiImage = [UIImage imageNamed:@"about_pingtai.png"] ;
    float Imageheigth = __kScreenWidth/3 * pingtaiImage.size.height/pingtaiImage.size.width ;
    UIImageView *pingtaiView = [[UIImageView alloc] initWithFrame:CGRectMake(__kScreenWidth/3, 10, __kScreenWidth/3, Imageheigth)] ;
    [pingtaiView setImage:pingtaiImage] ;
    [self.view addSubview:pingtaiView] ;
    
    //管理图片
    UIImage *sloganImage = [UIImage imageNamed:@"about_slogan.png"] ;
    //float sloganheight = __kScreenWidth/2 * sloganImage.size.height/sloganImage.size.width ;
    float sloganwidth = Imageheigth * sloganImage.size.width / sloganImage.size.height ;
    UIImageView *sloganView = [[UIImageView alloc] initWithFrame:CGRectMake((__kScreenWidth - sloganwidth) / 2, Imageheigth+30, sloganwidth, Imageheigth)] ;
    [sloganView setImage:sloganImage] ;
    [self.view addSubview:sloganView] ;
    
    //二维码图片
    float barcodeY = sloganView.frame.origin.y + sloganView.frame.size.height + 20 ;
    UIImageView *barcodeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(__kScreenWidth/3, barcodeY, __kScreenWidth/3, __kScreenWidth/3)] ;
    [self.view addSubview:barcodeImageView] ;
    [self qrcodeView:barcodeImageView withSize:__kScreenWidth/3] ;
    
    //扫二维码label
    UILabel *barcodeLabel = [[UILabel alloc] init] ;
    barcodeLabel.font = [UIFont fontWithName:@"AvenirLTStd-Light" size:16.0f] ;
    barcodeLabel.backgroundColor = [UIColor clearColor] ;
    barcodeLabel.text = @"扫二维码，下载包工婆APP" ;
    float labelY = barcodeImageView.frame.origin.y + barcodeImageView.frame.size.height + 20 ;
    barcodeLabel.frame = CGRectMake(0, labelY, __kScreenWidth, 18.0f) ;
    barcodeLabel.textAlignment = NSTextAlignmentCenter ;
    [self.view addSubview:barcodeLabel] ;
    
    //版本号
    UIButton *vBtn = [UIButton buttonWithType:UIButtonTypeCustom] ;
    float vBtnY = barcodeLabel.frame.origin.y + barcodeLabel.frame.size.height + 20 ;
    vBtn.frame = CGRectMake((__kScreenWidth - __kScreenWidth/2.5)/2, vBtnY, __kScreenWidth/2.5, 30) ;
    [vBtn setTitle:@"版本：1.0.0v" forState:UIControlStateNormal] ;
    vBtn.titleLabel.font = [UIFont fontWithName:@"AvenirLTStd-Light" size:16.0f] ;
    vBtn.backgroundColor = [UIColor blueColor] ;
    vBtn.layer.cornerRadius = 15.0f ;
    vBtn.enabled = NO ;
    [self.view addSubview:vBtn] ;
    
    //服务协议
    UIButton *serviceBtn = [UIButton buttonWithType:UIButtonTypeCustom] ;
    float serviceBtnY = vBtn.frame.origin.y + vBtn.frame.size.height + 20 ;
    serviceBtn.frame = CGRectMake((__kScreenWidth - __kScreenWidth/2)/2, serviceBtnY, __kScreenWidth/2, 40) ;
    [serviceBtn setTitle:@"服务协议" forState:UIControlStateNormal] ;
    [serviceBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal] ;
    serviceBtn.layer.cornerRadius = 5.0f ;
    [serviceBtn.layer setBorderWidth:0.5f] ;
    serviceBtn.layer.borderColor = [UIColor blackColor].CGColor ;
    [serviceBtn addTarget:self action:@selector(servicePage) forControlEvents:UIControlEventTouchUpInside] ;
    [self.view addSubview:serviceBtn] ;
    
    //服务热线
    UILabel *serviceLabel = [[UILabel alloc] init] ;
    serviceLabel.font = [UIFont fontWithName:@"AvenirLTStd-Light" size:16.0f] ;
    serviceLabel.backgroundColor = [UIColor clearColor] ;
    serviceLabel.text = @"服务热线 - 400 7880 889" ;
    float serviceLabelY = serviceBtn.frame.origin.y + serviceBtn.frame.size.height + 30 ;
    serviceLabel.frame = CGRectMake(0, serviceLabelY, __kScreenWidth, 18.0f) ;
    serviceLabel.textAlignment = NSTextAlignmentCenter ;
    [self.view addSubview:serviceLabel] ;
}

- (void)servicePage
{
    ServiceViewController *vc = [[ServiceViewController alloc] init] ;
    self.hidesBottomBarWhenPushed = YES ;
    [self.navigationController pushViewController:vc animated:NO] ;
    
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO ;
    self.navigationItem.title = @"包工婆" ;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES ;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)qrcodeView:(UIImageView *)_imgView withSize:(CGFloat) size
{
    
    //二维码滤镜
    
    CIFilter *filter=[CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    //恢复滤镜的默认属性
    
    [filter setDefaults];
    
    //将字符串转换成NSData
    
    NSData *data=[@"http://sdg.baogongpo.cn/downloads/index.php" dataUsingEncoding:NSUTF8StringEncoding];
    
    //通过KVO设置滤镜inputmessage数据
    
    [filter setValue:data forKey:@"inputMessage"];
    
    //获得滤镜输出的图像
    
    CIImage *outputImage=[filter outputImage];
    
    //将CIImage转换成UIImage,并放大显示
    
    _imgView.image=[self createNonInterpolatedUIImageFormCIImage:outputImage withSize:size];
    
    
    
    //如果还想加上阴影，就在ImageView的Layer上使用下面代码添加阴影
    
    _imgView.layer.shadowOffset=CGSizeMake(0, 0.5);//设置阴影的偏移量
    
    _imgView.layer.shadowRadius=1;//设置阴影的半径
    
    _imgView.layer.shadowColor=[UIColor blackColor].CGColor;//设置阴影的颜色为黑色
    
    _imgView.layer.shadowOpacity=0.3;
    
    
    
}



//改变二维码大小

- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size {
    
    CGRect extent = CGRectIntegral(image.extent);
    
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    
    // 创建bitmap;
    
    size_t width = CGRectGetWidth(extent) * scale;
    
    size_t height = CGRectGetHeight(extent) * scale;
    
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    
    CIContext *context = [CIContext contextWithOptions:nil];
    
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    
    CGContextScaleCTM(bitmapRef, scale, scale);
    
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    // 保存bitmap到图片
    
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    
    CGContextRelease(bitmapRef);
    
    CGImageRelease(bitmapImage);
    
    return [UIImage imageWithCGImage:scaledImage];
    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
